#include "Simulate_System.h"
#include <iostream>
using namespace std;

Simulate_System::Simulate_System( int disk_number )
{
	m_disks.resize(disk_number);
	for (int i=0; i<disk_number; ++i)
		m_disks[i] = new VDisk;

	m_accessers.resize(disk_number);
	for (int i=0; i<disk_number; ++i)
	{
		if(i<2)
			m_accessers[i] = new AccessGenerater(i, 2, 4);
		else
			m_accessers[i] = new AccessGenerater(i, 1, 3);
	}
	m_lru_stack = LRU_Stack(1,0,50);
	m_time = 0;
}

Simulate_System::Simulate_System( int disk_number, int pirority_disks, int p_type )
{
	m_disks.resize(disk_number);
	for (int i=0; i<disk_number; ++i)
		m_disks[i] = new VDisk;

	m_accessers.resize(disk_number);
	for (int i=0; i<disk_number; ++i)
	{
		if(i<pirority_disks)
			m_accessers[i] = new AccessGenerater(i, 2, 4);
		else
			m_accessers[i] = new AccessGenerater(i, 1, 3);
	}
	

	switch (p_type)
	{
	case 1:
		m_lru_stack = LRU_Stack(1,0,50); //LRU-PA
		break;
	case 2:
		m_lru_stack = LRU_Stack(0,0,50);//LRU
		break;
	case 3:
		m_lru_stack = LRU_Stack(1,1,50); //MRU-PA
		break;
	case 4:
		m_lru_stack = LRU_Stack(0,1,50); //MRU
		break;
	}
	
	m_time = 0;

}

Simulate_System::~Simulate_System( void )
{
	for (int i=0; i<m_disks.size(); ++i)
	{
		delete m_disks[i];
		delete m_accessers[i];
	}
}

void Simulate_System::diskactive( int id )
{
	int block_add = m_accessers[id]->getAccess();
	cout <<"Access \t"<<block_add<<"\t";
	real_accress = m_lru_stack.accessBlock(block_add,m_accessers[id]->is_Priority);
 	cout << "Disk Access:\t";
	if(real_accress == -1)
		cout << "None \t";
	else
		cout << real_accress <<"\t";

	if(real_accress > 0)
	{
		int did = real_accress/1000;
		m_disks[id]->access();
	}
	m_disks[id]->update();
	cout << "Power Coms: " << m_disks[id]->power_comsumption <<"\n"; 
}

void Simulate_System::update(std::ofstream& out)
{
	m_time++;
	cout << "Time = "<<m_time<<"\n";
	power_total = 0;
	for (int i=0; i<m_accessers.size(); ++i)
	{
		diskactive(i);
		power_total += m_disks[i]->power_comsumption;
		out << m_disks[i]->power_comsumption << " " << int(real_accress>0) << " ";
	}
	out << power_total<<"\n";
	cout << "Power All = " << power_total << "\n";
}

